package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import adapter.LunTanNewsAdapter;
import base.BaseFragment;
import bean.LunTanSchoolNewsBean;
import presenter.LunTanNewsPresenter;
import utils.ToastUtil;
import view.LunTanNewsView;

/**
 * Created by zhang'lin'lin on 2019/11/6.
 */

public class LunTanNewsFragment extends BaseFragment<LunTanNewsView, LunTanNewsPresenter> implements LunTanNewsView {
    private View view;
    private RecyclerView mRv;
    private SmartRefreshLayout mSmart;
    private List<LunTanSchoolNewsBean> news_list;
    private LunTanNewsAdapter newsAdapter;
    private int id;
    private int page;
    private int mPosition;

    @Override
    public void showToast(String msg) {
        ToastUtil.show(getContext(), msg);
    }

    @Override
    protected LunTanNewsPresenter bindPresenter() {
        return new LunTanNewsPresenter();
    }

    @Override
    protected void initData() {
       mPresenter.getData(id,page);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView(View view) {
        Bundle arguments = getArguments();
        id = arguments.getInt("id");
        mRv = (RecyclerView) view.findViewById(R.id.rv);
        mSmart = (SmartRefreshLayout) view.findViewById(R.id.smart);
        mRv.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        news_list=new ArrayList <>();
        newsAdapter = new LunTanNewsAdapter(news_list, getActivity());
        mRv.setAdapter(newsAdapter);
        mSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.getData(id,page);
                mSmart.finishLoadMore(true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                news_list.clear();
                mPresenter.getData(id,page);
                mSmart.finishRefresh(true);
            }
        });
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_luntannews;
    }

    public static BaseFragment getInstance(int id) {
        LunTanNewsFragment fragment = new LunTanNewsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onSuccess(List <LunTanSchoolNewsBean> lunTanSchoolNewsBeans) {
        news_list.addAll(lunTanSchoolNewsBeans);
        newsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String error) {
        ToastUtil.show(getActivity(),error);
    }
}
